<!--
 * @Date: 2025-03-24 09:41:15
 * @LastEditors: Csir 2215995096@qq.com
 * @LastEditTime: 2025-07-03 17:22:47
 * @FilePath: \yujin-ui-admin\src\App.vue
-->
<script lang="ts" setup>
import { isDark } from '@/utils/is'
import { useAppStore } from '@/store/modules/app'
import { useDesign } from '@/hooks/web/useDesign'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import routerSearch from '@/components/RouterSearch/index.vue'
import { useRouter } from 'vue-router'
import { onMounted } from 'vue'

const router = useRouter()


onMounted(() => {
  // 子应用通过 $wujie 获取 props
  if (window.$wujie) {
    const { info } = window.$wujie?.props
    console.log('子应用获取到的参数app.vue', info)

    const { token, url } = info
    // window.localStorage.setItem('token', JSON.stringify(token))
    // fetchData()
    router.push({
      path: url
    })
  }
})

window.$wujie?.bus.$on('update-props', (newProps) => {
  console.log('收到新props', newProps)
  
  const { token, url } = newProps.info
  // window.localStorage.setItem('token', JSON.stringify(token))
  // 更新子应用数据逻辑...
  console.log('访问子应用路由地址', url)

  router.push({
    path: url
  })
})

defineOptions({ name: 'APP' })

const { getPrefixCls } = useDesign()
const prefixCls = getPrefixCls('app')
const appStore = useAppStore()
const currentSize = computed(() => appStore.getCurrentSize)
const greyMode = computed(() => appStore.getGreyMode)
const { wsCache } = useCache()

// 根据浏览器当前主题设置系统主题色
const setDefaultTheme = () => {
  let isDarkTheme = wsCache.get(CACHE_KEY.IS_DARK)
  if (isDarkTheme === null) {
    isDarkTheme = isDark()
  }
  appStore.setIsDark(isDarkTheme)
}
setDefaultTheme()
</script>
<template>
  <ConfigGlobal :size="currentSize">
    <RouterView :class="greyMode ? `${prefixCls}-grey-mode` : ''" />
    <routerSearch />
  </ConfigGlobal>
</template>
<style lang="scss">
$prefix-cls: #{$namespace}-app;

.size {
  width: 100%;
  height: 100%;
}

html,
body {
  @extend .size;

  padding: 0 !important;
  margin: 0;
  overflow: hidden;
  background-color: #e6edf7;

  #app {
    @extend .size;
  }
}

.#{$prefix-cls}-grey-mode {
  filter: grayscale(100%);
}
</style>
